#include <windows.h>

#include "GCString.h"

namespace gcstock
{
    /////////////////////////////////////////////
    // 
    // purpose	:
    // input	: str is a string end with '\0'
    // output	:
    // return	: return index of first ch in str
    //
    /////////////////////////////////////////////
    GCINT GCFind(IN const GCCHAR* str, IN const GCCHAR ch)
    {
        int i = 0;
        
        while(str[i])
        {
            if(str[i] == ch)
            {
                return i;
            }
            ++i;
        }
        return -1;
    }


    void splitW(IN const GCCHAR splitWCHAR, IN const GCCHAR* pStr, OUT GCVArray<GCCHAR*>& strArray)
    {
        int len = GCSTRLEN(pStr);
        int tmpSize = 0;
        int strStartIdx = 0;
        
        GCCHAR* pChildStr = NULL;
        for(int i = 0 ;i < len ; ++i)
        {   
            if(pStr[i] == splitWCHAR)
            {
                pChildStr = NULL;
                pChildStr = new GCCHAR[tmpSize + 1];
                
                if(!pChildStr)
                {
                    while(!pChildStr)
                    {                        
                        try
                        {
                            pChildStr = new GCCHAR[tmpSize + 1];
                        }
                        catch (exception *e)
                        {
                        	gccout<<_GCT("CATCH exception:")<<e->what()<<gcendl;
                        }
                        Sleep(1);
                    }
                }
                
                memcpy(pChildStr, &pStr[strStartIdx], (tmpSize + 1) * sizeof(GCCHAR));
                pChildStr[tmpSize] = _GCT('\0');
                strArray.add(pChildStr);
                strStartIdx = i + 1;
                tmpSize = 0;
            }
            else
            {
                ++tmpSize;
            }
            
        }
        if(tmpSize != 0)
        {
            GCCHAR* pChildStr = NULL;
            pChildStr = new GCCHAR[tmpSize + 1];
            if(!pChildStr)
            {  
                while(!pChildStr)
                {
                    try
                    {
                        pChildStr = new GCCHAR[tmpSize + 1];
                    }
                    catch (exception *e)
                    {
                        gccout<<_GCT("CATCH exception:")<<e->what()<<gcendl;
                    }
                }
            }
            memcpy(pChildStr, &pStr[strStartIdx], tmpSize* sizeof(GCCHAR));
            pChildStr[tmpSize] = _GCT('\0');
            strArray.add(pChildStr);
        }
    }
}